package com.xjh.him.fragment.other;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.xjh.him.R;
import com.xjh.him.fragment.profile.UserProfileFragment;
import com.xjh.him.core.BaseFragment;
import com.xjh.him.databinding.FragmentSearchUserBinding;
import com.xjh.him.dto.friend.FriendDTO;
import com.xjh.him.dto.friend.FriendQueryResultDTO;
import com.xjh.him.utils.Utils;
import com.xjh.him.utils.XToastUtils;
import com.xuexiang.xaop.annotation.SingleClick;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.core.PageOption;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.searchview.MaterialSearchView;

import org.xjh.tool.Checks;


@Page(name = "查询用户", extra = R.drawable.ic_menu_search_white)
public class SearchUserFragment extends BaseFragment<FragmentSearchUserBinding> {


    @NonNull
    @Override
    protected FragmentSearchUserBinding viewBindingInflate(LayoutInflater inflater, ViewGroup container) {
        return FragmentSearchUserBinding.inflate(inflater, container, false);
    }

    @Override
    protected TitleBar initTitle() {
        TitleBar titleBar = super.initTitle();
        titleBar.setTitle("查询用户");
        titleBar.addAction(new TitleBar.ImageAction(R.drawable.ic_action_query) {

            @Override
            @SingleClick
            public void performAction(View view) {
                binding.searchView.showSearch();
            }
        });
        return titleBar;
    }

    @Override
    protected void initViews() {
        binding.searchView.setVoiceSearch(false);
        binding.searchView.setEllipsize(true);
        binding.searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                if (Checks.isEmpty(query) || query.length() != 11) {
                    XToastUtils.toast("手机号不合法");
                    return false;
                }
                String verifyUrl = Utils.getWebServerApiUrl(requireActivity(),
                        String.format(getString(R.string.api_friend_verify), query));
                SimpleCallBack<FriendQueryResultDTO> verifyCB = new SimpleCallBack<FriendQueryResultDTO>() {
                    @Override
                    public void onSuccess(FriendQueryResultDTO response) throws Throwable {
                        FriendDTO friendDTO = new FriendDTO();
                        friendDTO.setId(response.getUserDTO().getId());
                        friendDTO.setNickname(response.getUserDTO().getNickname());
                        friendDTO.setAvatarUrl(response.getUserDTO().getAvatarUrl());
                        friendDTO.setSignature(response.getUserDTO().getSignature());
                        PageOption.to(UserProfileFragment.class)
                                .setNewActivity(true)
                                .putSerializable(UserProfileFragment.KEY_FRIEND_DTO, friendDTO)
                                .putBoolean(UserProfileFragment.KEY_IS_FRIEND, response.getFriend())
                                .open(SearchUserFragment.this);
                        popToBack();
                    }

                    @Override
                    public void onError(ApiException e) {
                        XToastUtils.toast(e.getDisplayMessage());
                    }
                };
                Utils.doGet(verifyUrl, verifyCB);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        binding.searchView.setSubmitOnClick(true);
    }

    @Override
    public void onDestroyView() {
        if (binding.searchView.isSearchOpen()) {
            binding.searchView.closeSearch();
        }
        super.onDestroyView();
    }
}
